package com.shizhanzhe.gxb.home.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.home.bean.Merchantlist;
import com.shizhanzhe.gxb.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

;

/**
 * Created by YJ on 2018/10/23.
 */
public class StoreAdapter extends RecyclerView.Adapter<StoreAdapter.ViewHolder> {
    private Context mContext;
    private List<Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean> mData = new ArrayList<>();
    private StoreListener listener;

    public StoreAdapter(Context context) {
        mContext = context;
    }

    public void setListener(StoreListener listener) {
        this.listener = listener;
    }

    public void updateData(int page, List<Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean> dataSet) {
        if (dataSet != null && !dataSet.isEmpty()) {
            if (page == 1) {
                mData = dataSet;
            } else {
                mData.addAll(dataSet);
            }
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_merchantlist, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean bean = mData.get(position);
        if (!"".equals(bean.getImg_url()) && null != bean.getImg_url()) {
            Glide.with(mContext).load(bean.getImg_url()).into(holder.headimg);
        } else {
            holder.headimg.setImageResource(bean.getMore_img());
        }
        holder.name.setText(bean.getMall_name());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.click(bean);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.headimg)
        CircleImageView headimg;
        @BindView(R.id.name)
        TextView name;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface StoreListener {
        void click(Merchantlist.DataBean.MerchantListResponseBean.MallSearchInfoVoListBean item);
    }
}
